package com.blog.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blog.admin.dao.*;
import com.blog.admin.entity.*;
import com.blog.admin.service.SysUserService;
import com.blog.admin.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleBodyMapper articleBodyMapper;
    @Autowired
    private ArticleTagMapper articleTagMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageVo<SysUser> findUserDetail(JSONObject param) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        param.put("currentPage", param.getInteger("currentPage")-1);
        List<SysUser> sysUsers = sysUserMapper.findUserByConditionAndPage(param);
        ArrayList<JSONObject> results = new ArrayList<>();
        if (!sysUsers.isEmpty()) {
            for (SysUser sysUser : sysUsers) {
                JSONObject userInfo = new JSONObject(new LinkedHashMap<>());
                userInfo.put("id", sysUser.getId());
                userInfo.put("account", sysUser.getAccount());
                userInfo.put("nickName", sysUser.getNickname());
                userInfo.put("createDate", simpleDateFormat.format(new Date(sysUser.getCreateDate())));
                userInfo.put("lasLogin", simpleDateFormat.format(new Date(sysUser.getLastLogin())));
                userInfo.put("mobilePhoneNumber", sysUser.getMobilePhoneNumber());
                userInfo.put("address", sysUser.getAddress());
                userInfo.put("fansNum", sysUser.getFansNum());
                userInfo.put("personalProfile", sysUser.getPersonalProfile());
                results.add(userInfo);
            }
        }
        return PageVo.<SysUser>builder()
                .page(param.getInteger("currentPage"))
                .size(param.getInteger("pageSize"))
                .total(sysUsers.size())
                .values(sysUsers)
                .build();
    }

    @Override
    @Transactional
    public void deleteUserById(long id) {
        //删除sys_user中信息
        sysUserMapper.deleteById(id);
        //删除文章相关信息
        List<Article> articles = articleMapper.selectById(id);
        if (!articles.isEmpty()) {
            for (Article article : articles) {
                //删除文章body
                articleBodyMapper.deleteById(article.getBodyId());
                //删除文章tag
                LambdaQueryWrapper<ArticleTag> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(ArticleTag::getArticleId,article.getId());
                articleTagMapper.delete(wrapper1);
                //删除文章
                articleMapper.deleteById(article.getId());
            }
        }
        //删除评论相关信息
        LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLambdaQueryWrapper.eq(Comment::getAuthorId,id);
        commentMapper.delete(commentLambdaQueryWrapper);
        //删除标签相关信息

        //删除该用户的登录状态
    }

    @Override
    @Transactional
    public void updateUserById(JSONObject param) {
        sysUserMapper.updateById(param);
    }


}
